<template>
  <div v-loading="loading" class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="clickAdd(1, '0')"
          >添加权限</el-button>
        </div>
        <el-table :data="list" row-key="id" default-expand-all border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="clickAdd(2, row.id)"
              >添加权限点</el-button>
              <el-button
                size="small"
                type="text"
                @click="clickShow(row.id)"
              >查看</el-button>
              <el-button
                size="small"
                type="text"
                @click="delClick(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 弹窗 -->
      <!-- 新增/编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="title" @close="showCloseDialog">
        <!-- 表单内容 -->
        <el-form :model="formData" label-width="100px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用" prop="enVisible">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right">
            <el-button @click="showCloseDialog">取消</el-button>
            <el-button type="primary" @click="submitBtn">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  reqAddPermission,
  reqDelPermission,
  reqGetPermissionList,
  reqGetPermissionDetail,
  reqUpdatePermission
} from '@/api/permission'
import { transListToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      loading: false, // 控制全局loading
      showDialog: false, // 控制弹窗
      list: [],
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    // 自定义标题控制
    title() {
      return this.formData.pid === '0' ? '添加权限' : '查看权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {

    // 查看编辑权限(回显)
    async clickShow(id) {
      this.showDialog = true
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
    },
    // 删除权限(高危操作)
    delClick(id) {
      this.$confirm('此乃高危操作！是否继续删除', '温馨提醒')
        .then(async() => {
          await reqDelPermission(id)
          this.$message.success('删除权限点成功') // 提示
          this.getPermissionList() // 重新获得数据
        })
        .catch(() => {
          this.$message.info('取消删除操作')
        })
    },
    // 提交添加权限
    async submitBtn() {
      if (this.formData.id) {
        //  编辑更新权限
        await reqUpdatePermission(this.formData)
        this.$message.success('更新权限成功')
      } else {
        //  添加权限
        if (this.formData.type === '' || this.formData.pid === '') {
          this.$message.error('权限名称与权限识别符不能为空！！！！')
          return
        }
        await reqAddPermission(this.formData)
        this.$message.success('添加权限成功！！！')
      }
      this.showCloseDialog()
      this.getPermissionList()
    },
    // 添加权限
    clickAdd(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭弹窗
    showCloseDialog() {
      this.showDialog = false
      this.formData = {}
    },
    // 获取权限
    async getPermissionList() {
      this.loading = true
      const { data } = await reqGetPermissionList()
      const result = transListToTree(data, '0')
      this.list = result
      this.loading = false
    }
  }
}
</script>
